# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :user.py
# @Time      :2022/8/11 9:27
# @Author    :吕华龙
from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, marshal
from models.model import *
from models.fields import *
from utils.cache import cache

user_bp = Blueprint("user_bp", __name__, url_prefix="/user")

api = Api(user_bp)


# 用户好友视图
class UserFriendView(Resource):
    #         缓存5秒
    @cache.cached(5)
    def get(self):
        resp = reqparse.RequestParser()
        resp.add_argument("id")
        args = resp.parse_args()
        # 根据id去查找此用户的好友
        friend = FriendModel.query.filter(FriendModel.user_id == args['id']).all()
        print("----", friend)
        list_1 = []
        # 构造数据结构
        for i in friend:
            user = UserModel.query.filter(UserModel.id == i.frien_id).first()
            list_1.append({
                "username": user.username,
                "head": user.head
            })
        # 根据名字排序
        list_1.sort(key=lambda x: x["username"])
        return jsonify({'code': 200, 'msg': '获取成功', 'data': list_1})


# 显示某好友的朋友圈内容
class FriendCircleView(Resource):
    def get(self):
        resp = reqparse.RequestParser()
        resp.add_argument("user_id")
        resp.add_argument("friend_id")
        args = resp.parse_args()
        # 根据id查找朋友圈
        # user_circle = CircleModel.query.filter(CircleModel.user_id == args['friend_id']).all()
        user_friend = FriendModel.query.filter(FriendModel.user_id == args['user_id'],
                                               FriendModel.frien_id == args['friend_id']).first()
        # 判断有没有权限查看朋友圈
        if user_friend.type == 1:
            # circle_marshal = marshal(user_circle, friendcircle_fields)
            return jsonify({'code': 200, 'msg': '查看朋友圈成功'})
        else:
            return jsonify({'code': 204, 'msg': '没有权限访问朋友圈'})


# 返回朋友圈
class CircleView(Resource):
    def get(self):
        resp = reqparse.RequestParser()
        resp.add_argument("user_id")
        args = resp.parse_args()
        # 获取自己的朋友圈内容
        circle = CircleModel.query.filter(CircleModel.user_id == args['user_id']).order_by(
            CircleModel.release_time.desc()).all()
        user = UserModel.query.filter(UserModel.id == args['user_id']).first()
        print(">>>>>>>>", circle)
        # 循环获取数据构建数据结构
        list_1 = []
        for i in circle:
            list_1.append({
                "circle_content": i.circle_content,
                "release_time": i.release_time,
                "circle_img": i.circle_img
            })
        return jsonify({'code': 200, 'msg': '获取朋友圈内容成功', 'data': list_1, "username": user.username})


# 屏蔽好友
class ShieldFriendView(Resource):
    def post(self):
        resp = reqparse.RequestParser()
        resp.add_argument("user_id")
        resp.add_argument("friend_id")
        args = resp.parse_args()
        # 获取
        friend = FriendModel.query.filter(FriendModel.user_id == args['user_id'],
                                          FriendModel.frien_id == args['friend_id']).first()
        print("-----", friend.type, type(friend.type))
        # 屏蔽
        sql = FriendModel.query.filter(FriendModel.user_id == args['user_id'],
                                       FriendModel.frien_id == args['friend_id'])
        if friend.type == 1:
            sql.update({"type": 0})
            db.session.commit()
            return jsonify({'code': 200, 'msg': '屏蔽好友成功'})
        # 取消屏蔽
        else:
            sql.update({'type': 1})
            db.session.commit()
            return jsonify({'code': 200, 'msg': '取消屏蔽好友成功'})


# 创建路由
api.add_resource(UserFriendView, "/user_friend")
# 获取朋友圈
api.add_resource(FriendCircleView, "/user_circle")
# 屏蔽好友
api.add_resource(ShieldFriendView, "/shield_friend")
# 获取朋友圈内容
api.add_resource(CircleView, "/circle")
